import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import {
  Response,
  ResponseParam,
  UserError,
  CurrentError,
} from '../../common/enums/response';
import { ApiErrorCode } from '../../common/enums/api-error-code.enum';
import { Goods } from '../../entities/goods.entity';

@Injectable()
export class GoodsService {
  constructor() {}
  @InjectRepository(Goods) private readonly goods: Repository<Goods>;

  /**
   *
   * @description 创建商品
   * @param {any} param
   * @memberof GodsService
   */
  public async create(param): Promise<object> {
    try {
      delete param.id;
      const goodsInfo = {
        name: param.name,
        price: param.price,
        infoCode: param.infoCode,
        phone: param.phone,
        isYearVip: param.isYearVip,
        isSms: param.isSms,
        isPhone: param.isPhone,
        yearVipTime: param.yearVipTime,
        smsNumber: param.smsNumber,
        phoneNumber: param.phoneNumber,
      };
      await this.goods.save(goodsInfo);
      let responseParam: ResponseParam = {
        message: ApiErrorCode.LACK_SET_SUCCESS,
        data: null,
      };
      return Response(responseParam);
    } catch (error) {
      return CurrentError.LACK_SET_FAIL;
    }
  }

  /**
   *
   * @description 修改商品
   * @param {any} param
   * @memberof GodsService
   */
  public async put(param): Promise<object> {
    try {
      const goodsInfo = {
        name: param.name,
        price: param.price,
        infoCode: param.infoCode,
        phone: param.phone,
        isYearVip: param.isYearVip,
        isSms: param.isSms,
        isPhone: param.isPhone,
        isStop: param.isStop,
        yearVipTime: param.yearVipTime,
        smsNumber: param.smsNumber,
        phoneNumber: param.phoneNumber,
      };
      await this.goods.update({ id: param.id }, goodsInfo);
      let responseParam: ResponseParam = {
        message: ApiErrorCode.LACK_SET_SUCCESS,
        data: null,
      };
      return Response(responseParam);
    } catch (error) {
      return CurrentError.LACK_SET_FAIL;
    }
  }

  /**
   *
   * @description 删除商品
   * @param {number} id
   * @memberof GodsService
   */
  public async delete(id: number): Promise<object> {
    try {
      await this.goods.update({ id }, { isDel: true });
      let responseParam: ResponseParam = {
        message: ApiErrorCode.LACK_DEL_SUCCESS,
        data: null,
      };
      return Response(responseParam);
    } catch (error) {
      return CurrentError.LACK_DEL_FAIL;
    }
  }

  /**
   *
   * @description 查询商品详情
   * @param {number} id
   * @memberof GodsService
   */
  public async getInfoById(id: number): Promise<object> {
    try {
      const data: Goods = await this.goods.findOneBy({ id, isDel: false });
      let responseParam: ResponseParam = {
        message: ApiErrorCode.LACK_GET_SUCCESS,
        data,
      };
      return Response(responseParam);
    } catch (error) {
      return CurrentError.LACK_GET_FAIL;
    }
  }

  /**
   *
   * @description 查询商品列表
   * @param {number} page
   * @param {number} pageSize
   * @param {string} name
   * @memberof GodsService
   */
  public async getList(
    page: number,
    pageSize: number,
    name?: string,
    type?: number,
  ) {
    let listRep: any = this.goods.createQueryBuilder().where('isDel = false');
    if (type) {
      // 包年产品
      if (type === 1) {
        listRep.andWhere('isYearVip=true');
      }
      if (type === 2) {
        listRep.andWhere('isSms=true');
      }
      if (type === 3) {
        listRep.andWhere('isPhone=true');
      }
    }
    if (name) {
      listRep.andWhere('name like :name', { name: `%${name}%` });
    }
    const list: any = await listRep
      .orderBy('creatTime', 'DESC')
      .skip((page - 1) * pageSize)
      .take(pageSize)
      .getManyAndCount();
    let responseParam: ResponseParam = {
      message: ApiErrorCode.LACK_GET_SUCCESS,
      data: list[0],
      count: list[1],
    };
    return Response(responseParam);
  }
}
